﻿using log4net;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing.Printing;
using System.IO.Ports;
using Telerik.Windows.Controls;
using Telerik.Windows.Data;
using TruckScale.Models;
using TruckScale.Properties;

namespace TruckScale.Views
{
  /// <summary>
  /// Interaction logic for SSWeighView.xaml
  /// </summary>
  public partial class SSWeighView : RadWindow, INotifyPropertyChanged
  {
    //log
    private static readonly log4net.ILog log = LogManager.GetLogger("SSWeighView");
    //新增后同步刷新列表
    public event PropertyChangedEventHandler PropertyChanged;
    public string InfoBar { get; set; }
    public string Theme { get; set; } = Global._weighConfig["Theme"];
    public string MainFontSize { get; set; } = Global._weighConfig["MainFontSize"];

    private bool _manualPrinting;
    private string _weighControl;

    #region 称重串口属性
    public string SelectedProtocol { get; set; }
    public string[] ProtocolSource { get; set; } = new string[] { "耀华", "金钟" };
    public string SelectedPort { get; set; }
    public string[] PortSource { get; set; } = SerialPort.GetPortNames();
    public int SelectedBaudRate { get; set; }
    public List<int> BaudRateSource { get; set; } = new List<int>() { 600, 1200, 2400, 4800, 9600, 19200 };
    public Parity SelectedParity { get; set; }
    public EnumDataSource ParitySource => new EnumDataSource { EnumType = typeof(Parity) };
    public int SelectedDataBits { get; set; }
    public List<int> DataBitsSource { get; set; } = new List<int>() { 5, 6, 7, 8 };
    public int SelectedStopBits { get; set; }
    public List<int> StopBitsSource => new List<int>() { 1, 2 };
    #endregion

    #region 打印机属性
    public PrinterSettings.StringCollection PrinterList { get; set; } = PrinterSettings.InstalledPrinters;
    public ObservableCollection<string> WeighingOncePrinters { get; set; } = new ObservableCollection<string>();
    public ObservableCollection<string> WeighingFinishPrinters { get; set; } = new ObservableCollection<string>();
    public ObservableCollection<string> HandWeighingPrinters { get; set; } = new ObservableCollection<string>();
    public bool IsWeighingOncePrint { get; set; }
    public bool IsWeighingFinishPrint { get; set; }
    public bool PreviewPrint { get; set; }
    public bool ManualPrinting
    {
      get => _manualPrinting;
      set
      {
        _manualPrinting = value;
        if (value)
        {
          IsWeighingOncePrint = false;
          IsWeighingFinishPrint = false;
        }
      }
    }
    public int PrintCopies { get; set; }
    public int PaperWidth { get; set; }
    public int PaperHeight { get; set; }
    #endregion

    #region TTS属性
    public double SpeechSpeed { get; set; }
    public bool TTS1Enable { get; set; }
    public string TTS1Text { get; set; }
    public bool TTS2Enable { get; set; }
    public string TTS2Text { get; set; }
    public bool TTS3Enable { get; set; }
    public string TTS3Text { get; set; }
    public bool TTS4Enable { get; set; }
    public string TTS4Text { get; set; }
    public bool TTS5Enable { get; set; }
    public string TTS5Text { get; set; }
    #endregion
    public bool ShowSwitchWeighTemplate { get; set; }
    public string WeighBridgeName { get; set; }
    public bool HasBhPrefix { get; set; }
    public string BhPrefix { get; set; }
    public int WeighMode { get; set; }
    public string WeighControl
    {
      get => _weighControl;
      set
      {
        _weighControl = value;
        if (value == "Auto")
        {
          if (WeighMode != 2)
          {
            WeighMode = 2;
          }
          if (TTS4Enable)
          {
            TTS4Enable = false;
          }
        }
      }
    }
    public double StableDelay { get; set; }
    public decimal MinWeight { get; set; }
    public string WeighUnit { get; set; }
    public bool WeighUnitConverter { get; set; }
    public double WeighExpiryDate { get; set; }
    public SSWeighView()
    {
      InitializeComponent();
      this.DataContext = this;

      //称重串口
      SelectedProtocol = Global._weighConfig["Protocol"];
      string[] spParam = Global._weighConfig["WeighSP"].Split(',');
      SelectedPort = spParam[0];
      SelectedBaudRate = Convert.ToInt32(spParam[1]);
      SelectedParity = (Parity)Enum.Parse(typeof(Parity), spParam[2]);
      SelectedDataBits = Convert.ToInt32(spParam[3]);
      SelectedStopBits = Convert.ToInt32(spParam[4]);

      //打印机
      WeighingOncePrinters = new ObservableCollection<string>(Global._weighConfig["WeighingOncePrinters"].Split(','));
      WeighingFinishPrinters = new ObservableCollection<string>(Global._weighConfig["WeighingFinishPrinters"].Split(','));
      HandWeighingPrinters = new ObservableCollection<string>(Global._weighConfig["HandWeighingPrinters"].Split(','));
      IsWeighingOncePrint = Convert.ToBoolean(Global._weighConfig["IsWeighingOncePrint"]);
      IsWeighingFinishPrint = Convert.ToBoolean(Global._weighConfig["IsWeighingFinishPrint"]);
      PreviewPrint = Convert.ToBoolean(Global._weighConfig["PreviewPrint"]);
      PrintCopies = Convert.ToInt32(Global._weighConfig["PrintCopies"]);
      PaperWidth = Convert.ToInt32(Global._weighConfig["PaperWidth"]);
      PaperHeight = Convert.ToInt32(Global._weighConfig["PaperHeight"]);

      //TTS
      SpeechSpeed = Convert.ToDouble(Global._weighConfig["SpeechSpeed"]);
      TTS1Enable = Convert.ToBoolean(Global._weighConfig["TTS1Enable"]);
      TTS1Text = Global._weighConfig["TTS1Text"];
      TTS2Enable = Convert.ToBoolean(Global._weighConfig["TTS2Enable"]);
      TTS2Text = Global._weighConfig["TTS2Text"];
      TTS3Enable = Convert.ToBoolean(Global._weighConfig["TTS3Enable"]);
      TTS3Text = Global._weighConfig["TTS3Text"];
      TTS4Enable = Convert.ToBoolean(Global._weighConfig["TTS4Enable"]);
      TTS4Text = Global._weighConfig["TTS4Text"];
      TTS5Enable = Convert.ToBoolean(Global._weighConfig["TTS5Enable"]);
      TTS5Text = Global._weighConfig["TTS5Text"];

      //地磅名称
      WeighBridgeName = Global._weighConfig["WeighBridgeName"];
      //编号前缀
      HasBhPrefix = Convert.ToBoolean(Global._weighConfig["HasBhPrefix"]);
      BhPrefix = Global._weighConfig["BhPrefix"];
      //显示采购/销售按钮
      ShowSwitchWeighTemplate = Convert.ToBoolean(Global._weighConfig["ShowSwitchWeighTemplate"]);
      //显示打印按钮
      ManualPrinting = Convert.ToBoolean(Global._weighConfig["ShowPrintButton"]);
      //一次/两次称重
      WeighMode = Convert.ToInt32(Global._weighConfig["WeighMode"]);
      //手动/自动称重
      WeighControl = Global._weighConfig["WeighControl"];
      //稳定延时
      StableDelay = Convert.ToDouble(Global._weighConfig["StableDelay"]);
      //最小称重
      MinWeight = Convert.ToDecimal(Global._weighConfig["MinWeight"]);
      //称重单位
      WeighUnit = Global._weighConfig["WeighUnit"];
      //称重单位转换
      WeighUnitConverter = Convert.ToBoolean(Global._weighConfig["WeighUnitConverter"]);
      //未完成称重记录有效期
      WeighExpiryDate = Convert.ToDouble(Global._weighConfig["WeighExpiryDate"]);
    }

    protected override bool OnClosing()
    {
      Global._weighConfig["Protocol"] = SelectedProtocol;
      Global._weighConfig["WeighSP"] = $"{ SelectedPort},{SelectedBaudRate},{SelectedParity},{SelectedDataBits},{SelectedStopBits}";

      Global._weighConfig["WeighingOncePrinters"] = string.Join(",", WeighingOncePrinters);
      Global._weighConfig["WeighingFinishPrinters"] = string.Join(",", WeighingFinishPrinters);
      Global._weighConfig["HandWeighingPrinters"] = string.Join(",", HandWeighingPrinters);
      Global._weighConfig["IsWeighingOncePrint"] = IsWeighingOncePrint.ToString();
      Global._weighConfig["IsWeighingFinishPrint"] = IsWeighingFinishPrint.ToString();
      Global._weighConfig["PreviewPrint"] = PreviewPrint.ToString();
      Global._weighConfig["PrintCopies"] = PrintCopies.ToString();
      Global._weighConfig["PaperWidth"] = PaperWidth.ToString();
      Global._weighConfig["PaperHeight"] = PaperHeight.ToString(); ;

      Global._weighConfig["SpeechSpeed"] = SpeechSpeed.ToString();
      Global._weighConfig["TTS1Enable"] = TTS1Enable.ToString();
      Global._weighConfig["TTS1Text"] = TTS1Text;
      Global._weighConfig["TTS2Enable"] = TTS2Enable.ToString();
      Global._weighConfig["TTS2Text"] = TTS2Text;
      Global._weighConfig["TTS3Enable"] = TTS3Enable.ToString();
      Global._weighConfig["TTS3Text"] = TTS3Text;
      Global._weighConfig["TTS4Enable"] = TTS4Enable.ToString();
      Global._weighConfig["TTS4Text"] = TTS4Text;
      Global._weighConfig["TTS5Enable"] = TTS5Enable.ToString();
      Global._weighConfig["TTS5Text"] = TTS5Text;

      Global._weighConfig["WeighBridgeName"] = WeighBridgeName;
      Global._weighConfig["HasBhPrefix"] = HasBhPrefix.ToString();
      Global._weighConfig["BhPrefix"] = BhPrefix;
      Global._weighConfig["ShowSwitchWeighTemplate"] = ShowSwitchWeighTemplate.ToString();
      Global._weighConfig["ShowPrintButton"] = ManualPrinting.ToString();
      Global._weighConfig["WeighMode"] = WeighMode.ToString();
      Global._weighConfig["WeighControl"] = WeighControl;
      Global._weighConfig["StableDelay"] = StableDelay.ToString();
      Global._weighConfig["MinWeight"] = MinWeight.ToString();
      Global._weighConfig["WeighUnit"] = WeighUnit;
      Global._weighConfig["WeighUnitConverter"] = WeighUnitConverter.ToString();
      Global._weighConfig["WeighExpiryDate"] = WeighExpiryDate.ToString();

      //保存到数据库
      WeighConfigModel.UpdateConfig(Global._weighConfig);

      return base.OnClosing();
    }
  }
}
